﻿using HarfBuzzSharp;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using 基于WPF的电缆采样数据分析系统.Models.FileLogs;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.Service;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Tools.LogTools;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;
using 基于WPF的电缆采样数据分析系统.Models.FileLogs;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork.MenuPage
{
    public class LogPageUCViewModel : BindableBase
    {

        // 单例模型
        public static LogPageUCViewModel Instance = new LogPageUCViewModel(App.GetService<IRegionManager>(), App.GetService<IRegionNavigationJournal>());

        /// <summary>
        /// 区域管理器
        /// </summary>
        private readonly IRegionManager RegionManager;

        /// <summary>
        /// 历史记录
        /// </summary>
        private IRegionNavigationJournal Journal;

        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        /// <summary>
        /// 对话服务
        /// </summary>
        private readonly DialogHostService DialogHostService;


        #region 操作日志集合

        /// <summary>
        /// 操作日志集合
        /// </summary>
        private ObservableCollection<AllUsedLog> _UsedLogList;

        /// <summary>
        /// 操作日志集合
        /// </summary>
        public ObservableCollection<AllUsedLog> UsedLogList
        {
            get { return _UsedLogList; }
            set { 
                _UsedLogList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 索引集合
        /// </summary>
        private List<int> _IndexList = new List<int>();

        /// <summary>
        /// 索引集合
        /// </summary>
        public List<int> IndexList
        {
            get { return _IndexList; }
            set { _IndexList = value; }
        }

        #endregion


        /// <summary>
        /// 构造器
        /// </summary>
        public LogPageUCViewModel(IRegionManager _RegionManager, IRegionNavigationJournal _Journal)
        {

            RegionManager = _RegionManager;

            Journal = _Journal;

            Aggregator = App.GetService<IEventAggregator>();

            DialogHostService = App.GetService<DialogHostService>();

            // 给操作日志集合赋值
            UsedLogList = GlobalVariableEdit.UsedLogList;

            // 绑定选择要显示的列命令
            SelectTheColumnsToDisplayCmm = new DelegateCommand(SelectTheColumnsToDisplayFun);

            // 绑定导航报表分析区命令
            ReportAnalysisAreaCmm = new DelegateCommand(ReportAnalysisAreaFun);

            // 绑定导航历史数据区命令
            HistoricalDataAreaCmm = new DelegateCommand(HistoricalDataAreaFun);

            // 绑定导出显示的列的日志到表格命令
            ExportTheDisplayColumnLogsToTheTableCmm = new DelegateCommand(ExportTheDisplayColumnLogsToTheTableFun);

            // 绑定删除命令
            DeleteCmm = new DelegateCommand(DeleteFun);

            // 绑定删除所有命令
            DeleteAllCmm = new DelegateCommand(DeleteAllFun);
        }


        #region 选择要显示的列
        /// <summary>
        /// 选择要显示的列
        /// </summary>
        public DelegateCommand SelectTheColumnsToDisplayCmm { get; set; }

        /// <summary>
        /// 选择要显示的列
        /// </summary>
        private async void SelectTheColumnsToDisplayFun()
        {
            DialogParameters paras = new DialogParameters();

            List<bool> list = ColumnsStatus(AidVisibility, UidVisibility, TimeVisibility, OperateVisibility);

            paras.Add("Title", LangugeConvertor.GetValue("CheckTheShowColumnspage"));
            paras.Add("list", list);

            // 打开对话框
            var result = await DialogHostService.ShowDialog("SelectTheColumnsToDisplayUC", paras);

            if (result.Result == ButtonResult.Yes)
            {
                // 索引号
                if (result.Parameters.GetValue<bool>("IndexChecked"))
                {
                    AidVisibility = Visibility.Visible;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(0);
                } else
                {
                    AidVisibility = Visibility.Collapsed;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(1);
                }

                // 用户
                if (result.Parameters.GetValue<bool>("UserChecked"))
                {
                    UidVisibility = Visibility.Visible;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(2);
                }
                else
                {
                    UidVisibility = Visibility.Collapsed;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(3);
                }

                // 时间
                if (result.Parameters.GetValue<bool>("TimeChecked"))
                {
                    TimeVisibility = Visibility.Visible;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(4);
                }
                else
                {
                    TimeVisibility = Visibility.Collapsed;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(5);
                }

                // 操作描述
                if (result.Parameters.GetValue<bool>("OperateDescriptionChecked"))
                {
                    OperateVisibility = Visibility.Visible;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(6);
                }
                else
                {
                    OperateVisibility = Visibility.Collapsed;
                    Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Publish(7);
                }
            }
        }

        /// <summary>
        /// 列的状态
        /// </summary>
        private List<bool> ColumnsStatus(Visibility AidVisibility, Visibility UidVisibility, Visibility TimeVisibility, Visibility OperateVisibility)
        {
            var newBoolList = new List<bool>();

            // 索引号
            if (AidVisibility == Visibility.Visible)
            {
                newBoolList.Add(true);
            }
            else
            {
                newBoolList.Add(false);
            }

            // 用户
            if (UidVisibility == Visibility.Visible)
            {
                newBoolList.Add(true);
            }
            else
            {
                newBoolList.Add(false);
            }

            // 时间
            if (TimeVisibility == Visibility.Visible)
            {
                newBoolList.Add(true);
            }
            else
            {
                newBoolList.Add(false);
            }

            // 操作描述
            if (OperateVisibility == Visibility.Visible)
            {
                newBoolList.Add(true);
            }
            else
            {
                newBoolList.Add(false);
            }

            return newBoolList;
        }


        /// <summary>
        /// 索引号是否显示
        /// </summary>
        private Visibility _AidVisibility = Visibility.Visible;

        /// <summary>
        /// 索引号是否显示
        /// </summary>
        public Visibility AidVisibility
        {
            get { return _AidVisibility; }
            set { 
                _AidVisibility = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 用户id是否显示
        /// </summary>
        private Visibility _UidVisibility = Visibility.Visible;

        /// <summary>
        /// 用户id是否显示
        /// </summary>
        public Visibility UidVisibility
        {
            get { return _UidVisibility; }
            set { 
                _UidVisibility = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 时间是否显示
        /// </summary>
        private Visibility _TimeVisibility = Visibility.Visible;

        /// <summary>
        /// 时间是否显示
        /// </summary>
        public Visibility TimeVisibility
        {
            get { return _TimeVisibility; }
            set { 
                _TimeVisibility = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 操作描述是否显示
        /// </summary>
        private Visibility _OperateVisibility = Visibility.Visible;

        /// <summary>
        /// 操作描述是否显示
        /// </summary>
        public Visibility OperateVisibility
        {
            get { return _OperateVisibility; }
            set { 
                _OperateVisibility = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        #region 导航报表分析区
        /// <summary>
        /// 导航到报表分析区
        /// </summary>
        public DelegateCommand ReportAnalysisAreaCmm { get; set; }

        /// <summary>
        /// 导航到报表分析区
        /// </summary>
        private void ReportAnalysisAreaFun()
        {
            RegionManager.Regions["MainViewRegion"].RequestNavigate("AccountingReportUC",
                callback =>
                {
                    Journal = callback.Context.NavigationService.Journal; // 记录导航足迹
                });

            Aggregator.GetEvent<MsgNavicationTabulation>().Publish();
        }
        #endregion


        #region 导航历史数据区
        /// <summary>
        /// 导航历史数据区
        /// </summary>
        public DelegateCommand HistoricalDataAreaCmm { get; set; }

        /// <summary>
        /// 导航历史数据区
        /// </summary>
        private void HistoricalDataAreaFun()
        {
            RegionManager.Regions["MainViewRegion"].RequestNavigate("HistoricalDataUC",
                callback =>
                {
                    Journal = callback.Context.NavigationService.Journal; // 记录导航足迹
                });

            Aggregator.GetEvent<MsgNavicationHistoricalDataArea>().Publish();
        }
        #endregion


        #region 导出显示的列的日志到表格
        /// <summary>
        /// 导出显示的列的日志到表格
        /// </summary>
        public DelegateCommand ExportTheDisplayColumnLogsToTheTableCmm { get; set; }

        /// <summary>
        /// 导出显示的列的日志到表格
        /// </summary>
        private void ExportTheDisplayColumnLogsToTheTableFun()
        {

            IndexList.Clear();

            for (int i = 0; i < UsedLogList.Count; i++)
            {
                if (UsedLogList[i].IsSelected)
                {
                    IndexList.Add(UsedLogList[i].Aid);
                }
            }

            if (IndexList.Count == 0)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoLogIsSelectedInTheCurrentLogBox"));
                return;
            }

            ObservableCollection<AllUsedLog> newAllUsedLogs = new ObservableCollection<AllUsedLog>();

            for (int i = 0; i < UsedLogList.Count; i++)
            {
                if (UsedLogList[i].IsSelected)
                {
                    newAllUsedLogs.Add(UsedLogList[i]);
                }
            }

            RetResult retResult = GlobalLogTool.Instance.ExportTheDisplayColumnLogsToTheTable(newAllUsedLogs);

            if (retResult == RetResult.Success)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("TheLogFileIsExportedSuccessfullyProcedure"));
            }
            else
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("ExportingLogFilesHasBeenCancelledProcedure"));

            }
        }
        #endregion


        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        public DelegateCommand DeleteCmm { get; set; }

        /// <summary>
        /// 删除
        /// </summary>
        private async void DeleteFun()
        {
            DialogParameters paras = new DialogParameters();

            IndexList.Clear();

            for (int i = 0; i < UsedLogList.Count; i++)
            {
                if (UsedLogList[i].IsSelected)
                {
                    IndexList.Add(UsedLogList[i].Aid);
                }
            }

            if (IndexList.Count == 0)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoLogIsSelectedInTheCurrentLogBox"));
                return;
            }

            string logIdList = LangugeConvertor.GetValue("DeleteTheIndexNumberAs") +  " ";

            for (int i = 0; i < IndexList.Count; i++)
            {
                if (i == (IndexList.Count - 1))
                {
                    logIdList += IndexList[i] + " ";
                } else
                {
                    logIdList += IndexList[i] + "、 ";
                }
            }

            logIdList += LangugeConvertor.GetValue("Logs");

            paras.Add("Title", LangugeConvertor.GetValue("DeletePage"));
            paras.Add("Content", logIdList);

            // 打开对话框
            var result = await  DialogHostService.ShowDialog("DeletePageUC", paras);


            if (result.Result == ButtonResult.Yes)
            {
                for (int i = 0; i < IndexList.Count; i++)
                {
                    UsedLogList.Remove(UsedLogList.FirstOrDefault(a => a.Aid == IndexList[i]));
                }
            }


            // 刷新日志列表
            Refresh();
        }

        /// <summary>
        /// 刷新日志列表
        /// </summary>
        private void Refresh()
        {
            for (int i = 0; i < UsedLogList.Count; i++)
            {
                UsedLogList[i].Aid = i + 1;
            }
        }

        #endregion


        #region 删除所有
        /// <summary>
        /// 删除所有
        /// </summary>
        public DelegateCommand DeleteAllCmm { get; set; }

        /// <summary>
        /// 删除所有
        /// </summary>
        private async void DeleteAllFun()
        {

            if (UsedLogList.Count == 0)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("TheCurrentLogFrameHasNoLogs"));
                return;
            }

            DialogParameters paras = new DialogParameters();

            paras.Add("Title", LangugeConvertor.GetValue("DeleteAllPages"));
            paras.Add("Content", LangugeConvertor.GetValue("WhetherToDeleteAllLogs"));

            // 打开对话框
            var result = await DialogHostService.ShowDialog("DeletePageUC", paras);

            if (result.Result == ButtonResult.Yes)
            {
                UsedLogList.Clear();
            }
            
        }
        #endregion
    }
}
